package demo07

/**
 * @author xiaoZhao
 * @date 2022/7/26
 * @describe 模式匹配对象实例
 */
object Test04_MathObject {
  def main(args: Array[String]): Unit = {
    val student = new Student("alice", 15)

    //针对对象的实例进行匹配
    val result = student match {
      case Student("alice", 15) => "alice, 20"
      case _ => "else"
    }
  }
}

class Student(val name: String, val age: Int)

object Student {
  def apply(name: String, age: Int): Student = new Student(name, age)

  // 实现unapply
  def unapply(student: Student): Option[(String, Int)] = {
    if (student == null){
      None
    } else{
      Some(student.name,student.age)
    }
  }
}
